//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 22
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using ProxyHelpers.EWS;
using System.Net;

namespace GetUserOofSettings
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            EmailAddress emailAddress = new EmailAddress();
            emailAddress.Address = "ken.malcolmson@contoso.com";
            GetUserOofSettings(binding, emailAddress);
        }

        /// <summary>
        /// Example of using the GetUserOofSettingsRequest method to get OOF 
        /// settings for a user (Listing 22-16)
        /// </summary>
        /// <param name="binding">Binding to make the call with</param>
        /// <param name="userToSetOofFor">EmailAddress of the user
        /// to get  OOF settings for.</param>
        public static void GetUserOofSettings(
            ExchangeServiceBinding binding,
            EmailAddress userToGetOofFor)
        {
            GetUserOofSettingsRequest getUserOofRequest =
                new GetUserOofSettingsRequest();
            getUserOofRequest.Mailbox = userToGetOofFor;

            // Set the request and check for any errors
            //
            GetUserOofSettingsResponse getUserOofResponse =
                binding.GetUserOofSettings(getUserOofRequest);
            if (getUserOofResponse.ResponseMessage.ResponseClass !=
                ResponseClassType.Success)
            {
                Console.WriteLine(String.Format(
                    "Error in getting OOF settings.\r\n{0}\r\n{1}",
                    getUserOofResponse.ResponseMessage.ResponseCode,
                    getUserOofResponse.ResponseMessage.MessageText));
                return;
            }

            // Display the OOF settings we received
            //
            Console.WriteLine("User OOF Settings for {0}",
                (String.IsNullOrEmpty(getUserOofRequest.Mailbox.Name) ?
                    getUserOofRequest.Mailbox.Address :
                    getUserOofRequest.Mailbox.Name));

            Console.WriteLine("OOF State: " +
                getUserOofResponse.OofSettings.OofState);
            if (getUserOofResponse.OofSettings.OofState == OofState.Scheduled)
            {
                Console.WriteLine("\tDuration: {0} - {1}",
                    getUserOofResponse.OofSettings.Duration.StartTime,
                    getUserOofResponse.OofSettings.Duration.EndTime);
            }
            Console.WriteLine("Internal Reply:\r\n\t" +
                getUserOofResponse.OofSettings.InternalReply.Message);

            Console.WriteLine("External Audience: " +
                getUserOofResponse.OofSettings.ExternalAudience);
            if (getUserOofResponse.OofSettings.ExternalAudience !=
                ExternalAudience.None)
            {
                Console.WriteLine("External Reply:\r\n\t" +
                    getUserOofResponse.OofSettings.ExternalReply.Message);
            }
        }

    }
}
